<template>
    <Form ref="formInline" :model="formInline" :rules="ruleInline" >
        <FormItem prop="user">
            <Input type="text" v-model="formInline.username" placeholder="Username">
                <Icon type="ios-person-outline" slot="prepend"></Icon>
            </Input>
        </FormItem>
        <FormItem prop="password">
            <Input type="password" v-model="formInline.password" placeholder="Password">
                <Icon type="ios-lock-outline" slot="prepend"></Icon>
             </Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit('formInline')">Signin</Button>
        </FormItem>
    </Form>
</template>
<script>
    export default {
        data () {
            return {
                formInline: {
                    username: '',
                    password: ''
                },
                ruleInline: {
                    user: [
                        { required: true, message: '用户名不能为空', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '密码不能为空', trigger: 'blur' },
                        { type: 'string', min: 3, message: '密码最少3位!', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
           async handleSubmit(name) {
                // this.$refs[name].validate((valid) => {
                //     if (valid) {
                //         this.$Message.success('Success!');
                //     } else {
                //         this.$Message.error('Fail!');
                //     }
                // })
                let username=this.formInline.username;
                let password=this.formInline.password;        
               const res=await this.$http.get("/api/login",{params:{
                    username:username,
                    password:password
                }})
                // console.log(res);
                const {code,msg,token}=res.data;
                if(code===0){
                     this.$Message.success(msg);
                     this.$store.commit("setToken",token);
                    localStorage.setItem("token",token);
                    const {redirect}=this.$route.query;
                    // console.log(redirect);
                
                    this.$router.push(redirect)
                        
                }
            }
        }
    }
</script>
